// packageI/invoice/components/childInvoiceHeader/childInvoiceHeader.js
const app = getApp();
Component({
    options: {
      styleIsolation: 'shared'
    },
    properties: {
      bearerList: {
        type: Array,
        value: [],
        observer (list) {
          this.splitBearerList(list);
        },
      },
      loading: {
        type: Boolean,
        value: false
      },
      finshed: {
        type: Boolean,
        value: false
      }
    },
    data: {
      isBatchOperation: false,
      personalList: [],
      companyList: [],
      batchBearers: [],
      imgPath: app.globalData.imgPath,
    },

    lifetimes: {
      attached () {
        this._initData();
      },
    },

    pageLifetimes: {
      show () {
        this._initData();
      },
    },

    methods: {
      _initData () {
        this.setData({
          isBatchOperation: false,
          personalList: [],
          companyList: [],
          batchBearers: [],
        })
      },

      // 分割发票抬头列表
      splitBearerList (list) {
        if (!Array.isArray(list)) {
          return false;
        }
        let personalList = list.filter(item => (item.type == 0));
        let companyList = list.filter(item => (item.type == 1));
        this.setData({
          personalList: this.data.personalList.concat(personalList),
          companyList: this.data.companyList.concat(companyList)
        })
      },

      // 多选
      selectBolfun () {
        this.setData({
          batchBearers: [],
          isBatchOperation: !this.data.isBatchOperation
        });
      },

      // 多选发票抬头
      checkboxGroupChange (e) {
        this.setData({
          batchBearers: e.detail
        });
      },

      toggle (e) {
        const { id } = e.currentTarget.dataset;
        const checkbox = this.selectComponent(`.checkbox-${id}`);
        checkbox.toggle();
      },

      // 删除发票抬头
      deleteBearers () {
        let urlStr = app.getNetAddresss("plugin.invoice.frontend.rise.destory");
        app._getNetWork({
          url: urlStr,
          data: {
            ids: this.data.batchBearers.join(',')
          },
          success: (response) => {
            let res = response.data;
            app.tips(res.msg);
            if (res.result == 1) {
              this._initData();
              this.triggerEvent("deleteBearers");
              // this.getBearerData();
            }
          }
        })
      },

      // 添加发票抬头
      toInvoiceOperation (e) {
        let {item} = e.currentTarget.dataset;
        let linkUrl = "/packageI/invoice/invoiceOperation/invoiceOperation";
        if (item) {
          linkUrl = linkUrl + "?id=" + item.id; 
        }
        wx.navigateTo({
          url: linkUrl,
        })
      },
    }
})
